<template>
  <el-tree
    ref="tree"
    :props="treeProps"
    :indent="16"
    node-key="industryCode"
    :render-after-expand="true"
    :highlight-current="true"
    :check-on-click-node="true"
    :expand-on-click-node="false"
    :lazy="true"
    :load="loadSubData"
    @node-click="handleNodeClick"
  />
</template>

<script>
import { fetchIndustryList } from '@/api/base/common'

export default {
  data() {
    return {
      treeProps: {
        label: 'industryName',
        isLeaf: 'leaf'
      }
    }
  },
  methods: {
    loadSubData(node, resolve) {
      if (node.level === 0) {
        return resolve([{
          industryCode: '',
          industryName: '国民经济行业分类',
          pCode: '',
          leaf: false,
          children: []
        }])
      } else {
        fetchIndustryList(node.data.industryCode).then(response => {
          resolve(response.data)
        })
      }
    },
    handleNodeClick(data, node, other) {
      this.$emit('update:industrycode', data.industryCode)
      this.$emit('update:industryname', data.industryName)
    }
  }
}
</script>
